trigger OportunityAfterUpdate on Opportunity (After Update,After Insert) {
    
    if(StopRecursion.DoRun)
    {  
        StopRecursion.DoRun = false;
        Map<String,String> accountRecordTypes = new Map<String,String>{}; 
        Map<String,String> opportunityRecordTypes = new Map<String,String>{}; 
        List<Account> accToUpdate = new List<Account>();
        Set<Id> AccIdSet = new Set<Id>(); 
        Set<string> oppIdsForWebService =  new Set<String>();
       
        List<RecordType> r = [Select r.SobjectType, r.Name, r.IsActive, r.Id From RecordType r where ((Sobjecttype='Opportunity') AND (isActive =true) )];
   
        List<Opportunity> lst_OpportunityToUpdate = new List<Opportunity>();
        List<Account> lst_AccountToUpdate = new List<Account>();
                        
        Set<String> setOppIds = new Set<String>();
        
        for(Opportunity o: trigger.new)
            setOppIds.add(o.id);
            
        for(RecordType rt: r)
            opportunityRecordTypes.put(rt.Name,rt.Id);
    
        List<Opportunity> lst_Opportunity = [Select Id, Account.Id, Account.Student_ID__c,Student_ID__c,
                                            Account.recordtypeId, recordtypeId from Opportunity where id IN : setOppIds] ;
        
        for(integer i = 0; i < trigger.new.size(); i++)
        {
            
          System.debug('------AAAAAAAAA-------- '+ trigger.new[i]);
            if((trigger.new[i].StageName == 'Listo para pago' || trigger.new[i].StageName == 'Application Submitted'))
            {
                System.debug('++++++++++++Hor Bhau Kya' + trigger.new[i].recordtypeid);
              
                   
                    if(trigger.new[i].RecordTypeId==opportunityRecordTypes.get('ULA - Traditional Layout')|| trigger.new[i].RecordTypeId==opportunityRecordTypes.get('Plantilla Oportunidad ULA Trad. Pago'))
                    {
                        System.debug('################1####' );
                        if(!trigger.new[i].IsWebServiceCalled__c)
                        {
                            oppIdsForWebService.add(trigger.old[i].StageName + '-' + trigger.new[i].id  );System.debug('-----ZZZZZZZZz');
                        }    
                    }
                    else if(trigger.new[i].RecordTypeId==opportunityRecordTypes.get('ULA - Working Adults Layout')|| trigger.new[i].RecordTypeId==opportunityRecordTypes.get('Plantilla Oportunidad ULA PE Pago'))
                    {
                        System.debug('###############2#####' );
                        if (trigger.new[i].Student_Id__c != '' || trigger.new[i].Student_Id__c != null){
                              String codeNumber = StudentNumberLogic.getCheckDigitString(trigger.new[i].Student_Id__c);
                              if(trigger.new[i].Account.Student_Id__c == null)
                              {                               
                                lst_OpportunityToUpdate.add(new Opportunity(id= trigger.new[i].id,Digit_Verification__c=codeNumber));
                                for(Opportunity ac: lst_Opportunity)
                                    if(ac.Id == trigger.new[i].id)
                                        lst_AccountToUpdate.add(new Account(Id=ac.Account.Id,Digit_Verification__c=codeNumber ,Student_ID__c = trigger.new[i].Student_Id__c));
                             }
                             else
                             {
                                lst_OpportunityToUpdate.add(new Opportunity(id= trigger.new[i].id,Digit_Verification__c=codeNumber));
                                for(Opportunity ac: lst_Opportunity)
                                    if(ac.Id == trigger.new[i].id)
                                        lst_AccountToUpdate.add(new Account(Id=ac.Account.Id,Digit_Verification__c=codeNumber));
                             }      
                       }
                        
                   }
                        
            }// Application Submitted condition done
             
        }
         
        if(lst_OpportunityToUpdate.size()>0)
        {
            update lst_OpportunityToUpdate;
            update lst_AccountToUpdate;
        }    
        // Calling to Web Service
        if(oppIdsForWebService.size() > 0 && trigger.isUpdate && !System.isFuture()){
             System.debug('+++++++++++AAAAAAA+++++++');            
            IntegrationUtill.doCalloutEduMxService(oppIdsForWebService,false);
            //IntegrationUtill.callEduMxService(oppIdsForWebService);
            System.debug('+++++++++++hahehaha+++++++');
            }
     }        
}